<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 菜单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">ID</label>
                    <div class="layui-input-block">
                        <input type="text" name="id" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">菜单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="menuName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="power">权限分配</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>

            <!--<table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>-->
            <table class="layui-hide" id="test" lay-filter="demo"></table>
            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>

        </div>
    </div>
</div>


<script src="/static/layuiadmin/layui/layui.js"></script>
<script th:inline="none">
    layui.use('table', function () {
        var table = layui.table
            , form = layui.form
            , $ = layui.$;
        //监听锁定操作
        /*form.on('checkbox(lockDemo)', function (obj) {
            /!*layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);*!/
            $.ajax({
                url: '/click'
                , method: 'post'
                , data: {'id':this.value,'name':this.name,'checked':obj.elem.checked}
            });
            return false;
        });*/

        table.render({
            elem: '#test'
            , url: '/demo/table/menu/'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', width: 80, title: 'ID', sort: true}
                , {field: 'menuName', width: 140, title: '菜单名称'}
                , {field: 'parentName', width: 140, title: '父类菜单'}
                , {field: 'menuType', width: 140, title: '菜单类型'}
                , {field: 'url', width: 160, title: '菜单地址'}
                , {fixed: 'right', width: 178, align: 'center', toolbar: '#table-useradmin-webuser'}
            ]]
            , page: true
        });
    });
</script>

<!--<script type="text/html" id="checkboxTpl1">
    <input type="checkbox" name="enabled" value="{{d.id}}" title="可用" lay-filter="lockDemo" {{ d.enabled== true
           ? 'checked' : '' }}>
</script>
<script type="text/html" id="checkboxTpl2">
    <input type="checkbox" name="locked" value="{{d.id}}" title="锁定" lay-filter="lockDemo" {{ d.locked== true
           ? 'checked' : '' }}>
</script>-->
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'useradmin', 'table'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , admin = layui.admin;

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的要删除该菜单么，如果是一级菜单会将其子菜单删除', function (index) {
                    admin.req({
                        url: '/deleteMenuById'
                        , method: 'post'
                        , data: {"id": data.id}
                        , done: function (res) {
                            table.reload('test');
                            layer.msg('已删除');
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2
                    , title: '修改菜单'
                    , content: '/user/menu/update?id=' + data.id
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        layer.close(index);
                    }
                });
                /*layer.alert('编辑行：<br>'+ JSON.stringify(data))*/
            }
        });

        //监听搜索
        form.on('submit(LAY-user-front-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('test', {
                where: field
            });
        });


        //事件
        var active = {
            power: function () {
                layer.open({
                    type: 2
                    , title: '分配权限'
                    , content: '/user/menu/menuform2.html'
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'demo1'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            /*var field = data.field; //获取提交的字段
                            $.ajax({
                                url: '/add'
                                ,contentType:"application/json"
                                , method: 'post'
                                , dataType: 'json'
                                , data: JSON.stringify(field)
                                , done: function (res) {
                                    table.reload('test');
                                }
                            });
                            return true;*/
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('test'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
            , batchdel: function () {
                var checkStatus = table.checkStatus('test')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }


                layer.confirm('确定删除吗，如果是一级菜单会将其子菜单删除？', function () {
                    admin.req({
                        url: '/deleteMenus'
                        , contentType: "application/json"
                        , method: 'post'
                        , dataType: 'json'
                        , data: JSON.stringify(checkData)
                        , done: function (res) {
                            table.reload('test');
                            layer.msg('已删除');
                        }
                        //,……
                    });
                    //执行 Ajax 后重载
                    /*
                    admin.req({
                      url: 'xxx'
                      //,……
                    });
                    */

                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , title: '添加菜单'
                    , content: '/user/menu/menuform.html'
                    , maxmin: true
                    , area: ['500px', '450px']
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'demo1'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            /*var field = data.field; //获取提交的字段
                            $.ajax({
                                url: '/add'
                                ,contentType:"application/json"
                                , method: 'post'
                                , dataType: 'json'
                                , data: JSON.stringify(field)
                                , done: function (res) {
                                    table.reload('test');
                                }
                            });
                            return true;*/
                            //提交 Ajax 成功后，静态更新表格中的数据
                            //$.ajax({});
                            table.reload('test'); //数据刷新
                            layer.close(index); //关闭弹层
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
